package practice1_100;

import java.util.ArrayList;
import java.util.List;

public class Inorder {
    static int preValue=Integer.MIN_VALUE;
    //实现一个方法，判断是否为二叉搜索树
    public static boolean checkBST(TreeNode node){
        if(node==null)return true;

        //判断左侧是否是二叉搜索树
        boolean leftCheck=checkBST(node.left);
        //左侧非二叉搜索树
        if(!leftCheck){
            return false;
        }
        if(node.val>preValue){
            preValue= node.val;
        }
        //判断右侧是否是二叉搜索树
        return checkBST(node.right);
    }
    static ArrayList<Integer> list=new ArrayList<>();
    public static boolean checkBST1(TreeNode node){
        add(node);
        for (int i = 0; i < list.size()-1 ; i++) {
              if(list.get(i)>=list.get(i+1))return false;
        }
        return true;
    }
    public static void add(TreeNode node) {
        if(node==null)return ;
        add(node.left);
        list.add(node.val);
        add(node.right);
    }

    public static void main(String[] args) {
        TreeNode node=new TreeNode(1,new TreeNode(-1,new TreeNode(-3),new TreeNode(0)),new TreeNode(5,new TreeNode(3),new TreeNode(6)));
        boolean resA=checkBST(node);
        boolean resB=checkBST1(node);
        System.out.println(resA==resB);
        System.out.println("1");
        System.out.println("1");
        System.out.println("1");
        System.out.println("1");
        System.out.println("1");
    }
}
